//
//  UrbanRegistViewController.m
//  UrbanManagement
//
//  Created by Can on 18/4/8.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanRegistViewController.h"
#import "UrbanRegistTextFild.h"
#import "UrbanRegistCollectionView.h"
#import "UrbanBureauViewController.h"

@interface UrbanRegistViewController ()

@property (nonatomic,strong)UrbanRegistCollectionView *collectView;

@property (nonatomic,strong)NSString *phoneStr;//手机号
@property (nonatomic,strong)NSString *codeStr;//验证码
@property (nonatomic,strong)NSString *nameStr;//姓名
@property (nonatomic,strong)NSString *passStr;//密码
@property (nonatomic,strong)UrbanRegistTextFild *msgTextFild;//验证码

@property (nonatomic,strong)NSTimer *timer;//定时器
@property (nonatomic,assign)int s;//秒

@end

@implementation UrbanRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = NO;

    [self initTitleWithTitle:@"警员注册"];
    
    [self creatView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

-(void)creatView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 5*screenW_6)];
    lineView.backgroundColor = Color(247, 247, 247);
    [self.view addSubview:lineView];

    //手机号
    UrbanRegistTextFild *phoneTextFild= [[UrbanRegistTextFild alloc]initWithFrame:CGRectMake(15*screenW_6, 5*screenW_6, 0, 0) lableName:@"手机号" placeholder:@"请输入您的手机号" keyboardType:UIKeyboardTypeNumberPad maxNum:11 type:UrbanRegistTextFildPhoneType];
    [self.view addSubview:phoneTextFild];
    phoneTextFild.UrbanRegistTextFildblock = ^(NSString *phone){
        self.phoneStr = phone;
    };
    
    //验证码
    self.msgTextFild = [[UrbanRegistTextFild alloc]initWithFrame:CGRectMake(15*screenW_6, CGRectGetMaxY(phoneTextFild.frame) + 5*screenW_6, 0, 0) lableName:@"验证码" placeholder:@"请输入收到的验证码" keyboardType:UIKeyboardTypeNumberPad maxNum:0 type:UrbanRegistTextFildMsgType];
    [self.view addSubview:self.msgTextFild];
    [self.msgTextFild.btn addTarget:self action:@selector(sendMSg:) forControlEvents:UIControlEventTouchUpInside];
    self.msgTextFild.UrbanRegistTextFildblock = ^(NSString *msg){
        self->_codeStr = msg;
    };
    
    //姓名
    UrbanRegistTextFild *nameTextFild = [[UrbanRegistTextFild alloc]initWithFrame:CGRectMake(15*screenW_6, CGRectGetMaxY(self.msgTextFild.frame) + 5*screenW_6, 0, 0) lableName:@"姓名" placeholder:@"请输入您的姓名" keyboardType:UIKeyboardTypeDefault maxNum:0 type:UrbanRegistTextFildNameType];
    [self.view addSubview:nameTextFild];
    nameTextFild.UrbanRegistTextFildblock = ^(NSString *msg){
        self->_nameStr = msg;
    };
    
    //密码
    UrbanRegistTextFild *passwordTextFild = [[UrbanRegistTextFild alloc]initWithFrame:CGRectMake(15*screenW_6, CGRectGetMaxY(nameTextFild.frame) + 5*screenW_6, 0, 0) lableName:@"密码" placeholder:@"请输入您的密码" keyboardType:UIKeyboardTypeDefault maxNum:0 type:UrbanRegistTextFildPassWordType];
    [self.view addSubview:passwordTextFild];
    passwordTextFild.UrbanRegistTextFildblock = ^(NSString *msg){
        self.passStr = msg;
    };
    
    
    //部门
    UrbanRegistTextFild *departmentTextFild = [[UrbanRegistTextFild alloc]initWithFrame:CGRectMake(15*screenW_6, CGRectGetMaxY(passwordTextFild.frame) + 5*screenW_6, 0, 0) lableName:@"部门" placeholder:@"城管分局" keyboardType:UIKeyboardTypeDefault maxNum:0 type:UrbanRegistTextFildBranchOfficeType];
    [self.view addSubview:departmentTextFild];
    [departmentTextFild.btn addTarget:self action:@selector(goDepartment:) forControlEvents:UIControlEventTouchUpInside];
    
    //证件照片
    UILabel *passportPhoto = [TomamaUI createLabel:CGRectMake(15*screenW_6, CGRectGetMaxX(departmentTextFild.frame), 85*screenW_6, 15*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:15*screenW_6] textColor:Color(51, 51, 51) text:@"证件照片"];
    [self.view addSubview:passportPhoto];
    passportPhoto.center = CGPointMake(passportPhoto.center.x, CGRectGetMaxY(departmentTextFild.frame) + 17*screenW_6 + 15/2*screenW_6);

    
    _collectView = [[UrbanRegistCollectionView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(passportPhoto.frame) + 20*screenW_6, CGRectGetMaxY(departmentTextFild.frame) + 17*screenW_6, SCREENW - (CGRectGetMaxX(passportPhoto.frame) + 20*screenW_6), 200*screenW_6) number:4];
    [self.view addSubview:_collectView];
    
    //注册
    CGFloat height = NavBarHeight;
    
    UIButton *registBtn = [TomamaUI createButton:0 Frame:CGRectMake(15*screenW_6, SCREENH - 15*screenW_6 - 45*screenW_6 - height, SCREENW - 30*screenW_6, 45*screenW_6) targ:self sel:@selector(regist) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:16*screenW_6] title:@"注册" image:nil backGroundImage:@"blue_button2"];
    registBtn.layer.cornerRadius = 3*screenW_6;
    registBtn.layer.masksToBounds = YES;
    [self.view addSubview:registBtn];
}

-(void)sendMSg:(UIButton *)sendBtn//发送验证码
{
//    NSString * MOBILE = @"^((13[0-9])|(14[^4,\\D])|(15[^4,\\D])|(18[0-9]))\\d{8}$|^1(7[0-9])\\d{8}$";
//    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
//    if ([regextestmobile evaluateWithObject:_phoneStr] == YES){
//        NSLog(@"对");
//    }else{
//        NSLog(@"错");
//    }
    
    sendBtn.userInteractionEnabled = NO;
    if (_phoneStr.length==11) {//验证手机号
        
        _s = 60;
        [sendBtn setTitle:@"60s" forState:UIControlStateNormal];
        sendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        
//        [HttpRequest PostrequestUrl:@"/jeesite/appInterface/registInterface/sendVerificationCode" Dic:@{@"mobile":_phoneStr,@"userType":@"6"} completion:^(NSDictionary *responseDic) {
//            NSLog(@"====%@",responseDic[@"msg"]);
//            if (![responseDic[@"success"] boolValue]) {//请求成功
//                [HudHelper showHudWithMessage:responseDic[@"msg"]?responseDic[@"msg"]:@"验证码发送失败" toView:self.view];
//                [sendBtn setTitle:@"重发" forState:UIControlStateNormal];
//                sendBtn.userInteractionEnabled = YES;
//                [_timer invalidate];
//                _timer = nil;
//            }
//        }];
    }
    else//验证失败
    {
        [ToolViewVC addKeyAlterViewController:@"手机号码格式不正确" Controller:self];
        sendBtn.userInteractionEnabled = YES;
    }
}

-(void)onTimer//定时器
{
    _s--;
    NSString *timeStr = [NSString stringWithFormat:@"%ds",_s];
    [_msgTextFild.btn setTitle:timeStr forState:UIControlStateNormal];
    if (_s==0)//重置按钮 定时器
    {
        [_msgTextFild.btn setTitle:@"重发" forState:UIControlStateNormal];
        _msgTextFild.btn.userInteractionEnabled = YES;
        [_timer invalidate];
        _timer = nil;
        _s = 60;
    }
}


- (void)goDepartment:(UIButton *)btn
{
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *a1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [ac addAction:a1];
    
    for (int i = 0; i < BureauArr.count; i++) {
        UIAlertAction *a2 = [UIAlertAction actionWithTitle:BureauArr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [btn setTitle:BureauArr[i] forState:UIControlStateNormal];
        }];
        [ac addAction:a2];
    }

    [self presentViewController:ac animated:YES completion:nil];
    
//    UrbanBureauViewController *bureauView = [UrbanBureauViewController new];
//    bureauView.titleStr = @"部门";
//    bureauView.UrbanChoseType = UrbanChoseBureauType;
//    [self.navigationController pushViewController:bureauView animated:YES];
//    bureauView.UrbanBureauViewControllerBlock = ^(NSString *string){
//        [btn setTitle:string forState:UIControlStateNormal];
//    };
}

- (void)regist//注册
{
    if (_phoneStr.length == 0) {
        
        [ToolViewVC addKeyAlterViewController:@"请输入手机号" Controller:self];
        
    }else if (_phoneStr.length != 11){
        
        [ToolViewVC addKeyAlterViewController:@"手机号输入有误" Controller:self];
        
    }else if (_codeStr.length == 0){
        
        [ToolViewVC addKeyAlterViewController:@"请输入验证码" Controller:self];
        
    }else if (_nameStr.length == 0){
        
        [ToolViewVC addKeyAlterViewController:@"请输入姓名" Controller:self];
        
    }else if (_passStr.length == 0){
        
        [ToolViewVC addKeyAlterViewController:@"请输入密码" Controller:self];
        
    }else if (_passStr.length < 6){
        
        [ToolViewVC addKeyAlterViewController:@"密码不能小于6位" Controller:self];
        
    }else if (_collectView.photoArray.count == 0){
        
        [ToolViewVC addKeyAlterViewController:@"请添加证件照片" Controller:self];
        
    }else if (_collectView.photoArray.count < 4){
        
        [ToolViewVC addKeyAlterViewController:@"证件照片需要4张" Controller:self];
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
